package com.lq.ecoldchain.ui.mine.enter

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.CarInfoEntity
import com.lq.ecoldchain.entity.CertificationEntity
import com.lq.ecoldchain.entity.CityInfo
import com.lq.ecoldchain.entity.EnterInfoBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.ContentUtils
import com.lq.ecoldchain.util.toast
import okhttp3.ResponseBody

/*
*2018/12/27 0027  10:21
*function by lq
*/
class EnterViewModel : BaseViewModel() {
    val date by lazy { MutableLiveData<String>() }
    val phone by lazy { MutableLiveData<String>() }
    val name by lazy { MutableLiveData<String>() }
    val sex by lazy { MutableLiveData<Boolean>() }
    val isSuccess by lazy { MutableLiveData<Boolean>() }
    private val map by lazy { HashMap<String, String>() }
    val enterInfo by lazy { MutableLiveData<EnterInfoBean>() }

    val isIdentity by lazy { MutableLiveData<String>() }//是否实名认证
    val isCarInfo by lazy { MutableLiveData<String>() }//是否上传货车信息
    val isCardUpdate by lazy { MutableLiveData<String>() }//司机证件
    val serviceLine by lazy { MutableLiveData<String>() }//是否上传服务路线

    init {
        sex.value = true
        date.value = "暂未设置"
        obtainData()
        obtainCarInfoData()
        obtainIdentityData()
        obtainDriverCardList()
        obtainServiceLine()
    }

    fun commit() {
        map.clear()
        when(name.value.isNullOrEmpty()|| phone.value.isNullOrEmpty()|| date.value=="暂未设置"){
            true ->{
                toast { "请填写完整信息" }}
            false->{
                if(sex.value==false){
                    map["gender"] = "2"
                }else{
                    map["gender"] = "1"
                }
                map["name"] = name.value!!
                map["birthDate"] = date.value!!
                map["phone"] = phone.value!!
                BaseNetCore.ApiServiceImpl.enterApply(map).compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<String>(){
                    override fun onSuccess(t: String?) {
                        isSuccess.value = true
                        toast { "提交成功" }
                    }
                    override fun onFail(code: String) {}
                })
            }
        }
    }

    /*获取入住信息*/
    private fun obtainData() {
        BaseNetCore.ApiServiceImpl.enterBasicData().compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<EnterInfoBean>() {
            override fun onSuccess(t: EnterInfoBean?) {
                enterInfo.value = t
                if(t!=null){
                    name.value=t.name
                    phone.value = t.phone
                    date.value = ContentUtils.long2Time(t.birthDate)
                    if(!t.feedback.isNullOrEmpty()){
                        toast { t.feedback }
                    }
                    if(t.gender==1){
                        sex.value = true
                    }
                }
            }

            override fun onFail(code: String) {

            }
        })
    }

    /*获取实名认证信息*/
   private fun obtainIdentityData(){
        BaseNetCore.ApiServiceImpl.getCertification().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<CertificationEntity>(){
            override fun onSuccess(t: CertificationEntity?) {
                if(t==null){
                    isIdentity.value = "未认证"
                }else{
                    isIdentity.value = "已认证"
                }
            }
            override fun onFail(code: String) {}
        })
    }

    /*获取货车信息*/
    private fun obtainCarInfoData(){
        BaseNetCore.ApiServiceImpl.getTruck().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<CarInfoEntity>(){
            override fun onSuccess(t: CarInfoEntity?) {
                if(t==null){
                    isCarInfo.value = "未上传"
                }else{
                    isCarInfo.value = "已上传"
                }
            }
            override fun onFail(code: String) {}
        })
    }

    /*获取证件列表*/
    private fun obtainDriverCardList(){
        BaseNetCore.ApiServiceImpl.driverCertificationList().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<List<CertificationEntity>>(){
            override fun onSuccess(t: List<CertificationEntity>?) {
                if(t.isNullOrEmpty()){
                    isCardUpdate.value = "未上传"
                }else{
                    isCardUpdate.value = "已上传"
                }
            }
            override fun onFail(code: String) {
                isCardUpdate.value = "未上传"
            }
        })
    }

    /*获取服务路线*/
    private fun obtainServiceLine(){
        BaseNetCore.ApiServiceImpl.obtainCarAllLine().compose(RxTransformer.switchSchedulers()).subscribe(object:BaseObserver<List<CityInfo>>(){
            override fun onSuccess(t: List<CityInfo>?) {
                if(t.isNullOrEmpty()){
                    serviceLine.value = "未添加"
                }else{
                    serviceLine.value = "已添加"
                }
            }
            override fun onFail(code: String) {
                serviceLine.value = "未添加"
            }
        })
    }
}